﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CadStrategy2013.CgqStyle;
using Infragistics.Win.UltraWinGrid;
using MyTool.FileOpt;

namespace cad2013
{
    public partial class HuanXianForm2 : Form
    {
        #region 绑定数据

        private int zlbsCount;
        private int jlbsCount;
        private int zldlCount;
        private int jldlCount;
        private int ypfsCount;
        private int ypjsCount;
        private int dmhCount;
        private int gybdcCount;
        private int zlCount;
        private int bzdCount;

        #endregion 绑定数据

        private List<HuanXianClass> hxclassList;
        private List<string> portList;
        private Dictionary<string, TongJiClass> tongjiDic;
        private TongJiClass tongJiFile;
        private Dictionary<string, string> strDic;

        private string[] strTj = new string[] { "直流道岔表示", "交流道岔表示", "直流道岔电流", "交流道岔电流", "无绝缘发送",
                                       "无绝缘接收","站内电码化","半自动","站联","高压不对称"};

        private string[] strShuXing = new string[] { "Izldb", "Jldb", "Izldl", "Ijldl", "Iypfs", "Iypjs", "Idmh", "Ibzd", "Izl", "Igybdc" };

        private IniFile inifileTj;

        private IniFile inifileDy;

        private IniFile inifileData;

        private string m_path;

        public HuanXianForm2(string path, string ver = "v1")
        {
            InitializeComponent();
            inifileTj = new IniFile(path + "\\统计.txt");
            inifileTj.Load();
            inifileDy = new IniFile(path + "\\dyhx.ini");
            inifileDy.Load();
            inifileData = new IniFile(path + "\\datahx.ini");
            inifileData.Load();
            m_path = path;

            #region old

            zldblabel.Text = (zlbsCount = inifileTj.GetInt("直流道岔表示", "数量", 0)).ToString();
            jldblabel.Text = (jlbsCount = inifileTj.GetInt("交流道岔表示", "数量", 0)).ToString();
            zldllabel.Text = (zldlCount = inifileTj.GetInt("直流道岔电流", "数量", 0)).ToString();
            jldllabel.Text = (jldlCount = inifileTj.GetInt("交流道岔电流", "数量", 0)).ToString();
            ypfslabel.Text = (ypfsCount = inifileTj.GetInt("无绝缘发送", "数量", 0)).ToString();
            ypjslabel.Text = (ypjsCount = inifileTj.GetInt("无绝缘接收", "数量", 0)).ToString();
            bzdlabel.Text = (bzdCount = inifileTj.GetInt("半自动", "数量", 0)).ToString();
            zllabel.Text = (zlCount = inifileTj.GetInt("站联", "数量", 0)).ToString();
            gybdclabel.Text = (gybdcCount = inifileTj.GetInt("高压不对称", "数量", 0)).ToString();
            dmhlabel.Text = (dmhCount = inifileTj.GetInt("站内电码化", "数量", 0)).ToString();

            #endregion old

            hxclassList = new List<HuanXianClass>();
            strDic = new Dictionary<string, string>();
            for (int i = 0; i < 10; i++)
            {
                strDic.Add(strShuXing[i], strTj[i]);
                strDic.Add(strTj[i], strShuXing[i]);
            }
        }

        public HuanXianForm2(string path)
        {
            InitializeComponent();
            tongjiDic = new Dictionary<string, TongJiClass>();
            strDic = new Dictionary<string, string>();
            for (int i = 0; i < 10; i++)
            {
                strDic.Add(strShuXing[i], strTj[i]);
                strDic.Add(strTj[i], strShuXing[i]);
            }
            inifileTj = new IniFile(path + "\\统计.txt");
            inifileTj.Load();
            inifileDy = new IniFile(path + "\\dyhx.ini");
            inifileDy.Load();
            inifileData = new IniFile(path + "\\datahx.ini");
            inifileData.Load();
            m_path = path;
            hxclassList = new List<HuanXianClass>();
            portList = new List<string>();
            // int total = 0;

            #region 读统计配置文件

            ICollection<string> list = inifileTj.GetAllSectionName();

            string[] strs = new string[list.Count];// GetSectionNames();
            int i1 = 0;
            foreach (string s in list)
            {
                strs[i1++] = s;
            }
            foreach (string str in strs)
            {
                TongJiClass tj = new TongJiClass();
                tj.Count = inifileTj.GetInt(str, "数量", 0);
                string[] str1s = inifileTj.GetString(str, "编号").Split(new char[] { '-', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                int n;
                try
                {
                    int.TryParse(str1s[1], out n);
                    tj.TongXingJiPort = str1s[0];
                    if (portList.Contains(str1s[0]) == false)
                        portList.Add(str1s[0]);
                    tj.CgqNo = n;
                }
                catch
                {
                    tj.TongXingJiPort = "";
                    tj.CgqNo = 0;
                }
                // total += tj.Count;
                tongjiDic.Add(str, tj);
            }
            if (tongjiDic.ContainsKey("高压不对称") == false)
            {
                TongJiClass tj = new TongJiClass();
                tongjiDic.Add("高压不对称", tj);
            }
            WriteDyhx(portList.Count);

            #endregion 读统计配置文件

            #region old

            zldblabel.Text = tongjiDic["直流道岔表示"].Count.ToString();
            jldblabel.Text = tongjiDic["交流道岔表示"].Count.ToString();
            zldllabel.Text = tongjiDic["直流道岔电流"].Count.ToString(); ;
            jldllabel.Text = tongjiDic["交流道岔电流"].Count.ToString();
            ypfslabel.Text = tongjiDic["无绝缘发送"].Count.ToString();
            ypjslabel.Text = tongjiDic["无绝缘接收"].Count.ToString();
            bzdlabel.Text = tongjiDic["半自动"].Count.ToString();
            zllabel.Text = tongjiDic["站联"].Count.ToString();
            gybdclabel.Text = tongjiDic["高压不对称"].Count.ToString();
            dmhlabel.Text = tongjiDic["站内电码化"].Count.ToString();

            #endregion old
        }

        private void WriteDyhx(int total)
        {
            //int portNo = (total - 1) / 32;
            //int cgqCount = (total - 1) % 32;
            inifileData.WriteInt("通信机接口", "sum", total);
            for (int i = 1; i <= total; i++)
            {
                string tongXinJiPort = GetTongXinJiPort(i);

                int n = inifileTj.GetInt("port" + tongXinJiPort, "数量", 0);
                inifileData.WriteString("接口" + i, "智能传感器编号", tongXinJiPort + "-2," + n);
                //if (i < (portNo + 1))
                //{
                //    inifileData.WriteValue("接口" + i, "智能传感器编号", tongXinJiPort + "-2," + 32);
                //}
                //else
                //{
                //    inifileData.WriteValue("接口" + i, "智能传感器编号", tongXinJiPort + "-2," + cgqCount);
                //}
            }
        }

        private string GetTongXinJiPort(int i)
        {
            string str = "";
            switch (i)
            {
                case 1:
                    str = "I";
                    break;

                case 2:
                    str = "II";
                    break;

                case 3:
                    str = "III";
                    break;

                case 4:
                    str = "IV";
                    break;
            }
            return str;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // object r = ultraGrid1.Rows[1].ListObject;
            RowsCollection r = ultraGrid1.Rows;
            Selected s = ultraGrid1.Selected;
            foreach (UltraGridRow r1 in s.Rows)
            {
                hxclassList.Remove(r1.ListObject as HuanXianClass);
            }
            ultraGrid1.SetDataBinding(hxclassList, "", false);
        }

        private void GetLable_value(string key, out Label lable, out int value)
        {
            lable = null;
            value = 0;
            switch (key)
            {
                case "Ibzd":
                    lable = bzdlabel;
                    value = bzdCount;
                    break;
                //case "IdymokuaiNo":
                //    lable = dylabel;
                //    value = tongjiDic[strDic[key]].Count;
                //    break;
                case "Igybdc":
                    lable = gybdclabel;
                    value = gybdcCount;
                    break;

                case "Ijldl":
                    lable = jldllabel;
                    value = jldlCount;
                    break;

                case "Iypfs":
                    lable = ypfslabel;
                    value = ypfsCount;
                    break;

                case "Izl":
                    lable = zllabel;
                    value = zlCount;
                    break;

                case "Izldb":
                    lable = zldblabel;
                    value = zlbsCount;
                    break;

                case "Izldl":
                    lable = zldllabel;
                    value = zldlCount;
                    break;

                case "Jldb":
                    lable = jldblabel;
                    value = jlbsCount;
                    break;

                case "Iypjs":
                    lable = ypjslabel;
                    value = ypjsCount;
                    break;

                case "Idmh":
                    lable = dmhlabel;
                    value = dmhCount;
                    break;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int n;
            int startno;
            int.TryParse(TianJiatextBox.Text, out n);
            int.TryParse(StartNotextBox.Text, out startno);
            for (int i = 0; i < n; i++)
            {
                HuanXianClass hx = new HuanXianClass();
                hx.IdymokuaiNo = startno++;
                hxclassList.Add(hx);
            }
            ultraGrid1.SetDataBinding(hxclassList, "", false);
        }

        private string GetNextPort(string port)
        {
            string str = "";
            switch (port)
            {
                case "I":
                    str = "II";
                    break;

                case "II":
                    str = "III";
                    break;

                case "III":
                    str = "IV";
                    break;
            }
            return str;
        }

        private string[] GetZnCgqString(int count, string propName, string[] strResule)
        {
            string port;
            int n;
            string strkey;
            strkey = strDic[propName];
            n = tongjiDic[strkey].CgqNo;
            port = tongjiDic[strkey].TongXingJiPort;
            string strTemp = GetJkbString(count, propName);
            if (n + count <= 33)
            {
                strResule[0] += port + "-" + n + "," + count + "," + strkey + ";";
                if (strTemp != "")
                {
                    strResule[1] += port + "-" + n + strTemp + "," + count + ";";
                }
                if (n + count == 33)
                {
                    tongjiDic[strkey].CgqNo = 2;
                    tongjiDic[strkey].TongXingJiPort = GetNextPort(port);
                }
                else
                {
                    tongjiDic[strkey].CgqNo += count;
                }
            }
            else
            {
                int n1 = (n + count) - 34;
                strResule[0] += port + "-" + n + "," + (count - n1) + "," + strkey + ";";
                if (strTemp != "")
                {
                    strResule[1] += port + "-" + n + strTemp + "," + (count - n1) + ";";
                    strResule[1] += GetNextPort(port) + "-2" + strTemp + "," + n1 + ";";
                }
                strResule[0] += GetNextPort(port) + "-2," + n1 + "," + strkey + ";";
                tongjiDic[strkey].CgqNo = 2 + n1;
                tongjiDic[strkey].TongXingJiPort = GetNextPort(port);
            }
            return strResule;
        }

        private string[] GetZnCgqString(HuanXianClass hx)// string  strCgqResule,out string strJkbResule)
        {
            //strCgqResule = "";
            //strJkbResule = "";
            string[] strs = new string[2];
            if (hx.Ibzd != 0)
            {
                strs = GetZnCgqString(hx.Ibzd, "Ibzd", strs);
            }
            if (hx.Idmh != 0)
            {
                strs = GetZnCgqString(hx.Idmh, "Idmh", strs);
            }
            if (hx.Igybdc != 0)
            {
                strs = GetZnCgqString(hx.Igybdc, "Igybdc", strs);
            }
            if (hx.Ijldl != 0)
            {
                strs = GetZnCgqString(hx.Ijldl, "Ijldl", strs);
            }
            if (hx.Iypfs != 0)
            {
                strs = GetZnCgqString(hx.Iypfs, "Iypfs", strs);
            }
            if (hx.Iypjs != 0)
            {
                strs = GetZnCgqString(hx.Iypjs, "Iypjs", strs);
            }
            if (hx.Izl != 0)
            {
                strs = GetZnCgqString(hx.Izl, "Izl", strs);
            }
            if (hx.Izldb != 0)
            {
                strs = GetZnCgqString(hx.Izldb, "Izldb", strs);
            }
            if (hx.Izldl != 0)
            {
                strs = GetZnCgqString(hx.Izldl, "Izldl", strs);
            }
            if (hx.Jldb != 0)
            {
                strs = GetZnCgqString(hx.Jldb, "Jldb", strs);
            }
            if (strs[0] == null)
                strs[0] = "";
            if (strs[1] == null)
                strs[1] = "";
            return strs;
        }

        private string[] GetZnCgqString(HuanXianClass hx, TongJiClass tj)
        {
            string[] strs = new string[2];
            string str1 = "";
            string str2 = "";
            string[] strs2;
            if (hx.Ibzd != 0)
            {
                strs2 = tj.GetCgqBianHao("半自动", hx.Ibzd);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
                //strs = GetZnCgqString(hx.Ibzd, "Ibzd", strs);
            }
            if (hx.Idmh != 0)
            {
                strs2 = tj.GetCgqBianHao("站内电码化", hx.Idmh);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
            }
            if (hx.Igybdc != 0)
            {
                strs2 = tj.GetCgqBianHao("高压不对称", hx.Igybdc);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
            }
            if (hx.Ijldl != 0)
            {
                strs2 = tj.GetCgqBianHao("交流道岔电流", hx.Ijldl);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
            }
            if (hx.Iypfs != 0)
            {
                strs2 = tj.GetCgqBianHao("无绝缘发送", hx.Iypfs);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
            }
            if (hx.Iypjs != 0)
            {
                strs2 = tj.GetCgqBianHao("无绝缘接收", hx.Iypjs);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
            }
            if (hx.Izl != 0)
            {
                strs2 = tj.GetCgqBianHao("站联", hx.Izl);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
            }
            if (hx.Izldb != 0)
            {
                strs2 = tj.GetCgqBianHao("直流道岔表示", hx.Izldb);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
            }
            if (hx.Izldl != 0)
            {
                strs2 = tj.GetCgqBianHao("直流道岔电流", hx.Izldl);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
            }
            if (hx.Jldb != 0)
            {
                strs2 = tj.GetCgqBianHao("交流道岔表示", hx.Jldb);
                str1 = str1 + strs2[0];
                str2 = str2 + strs2[1];
            }
            strs[0] = str1;
            strs[1] = str2;
            return strs;
        }

        private string GetJkbString(int count, string propName)
        {
            string strjk = "";
            string port;
            int n;
            string strkey;
            string strResule = "";
            strkey = strDic[propName];
            n = tongjiDic[strkey].CgqNo;
            port = tongjiDic[strkey].TongXingJiPort;
            if (propName == "Izldl" || propName == "Ijldl" || propName == "Iypfs"
                || propName == "Iypjs" || propName == "Idmh" || propName == "Ibzd")
            {
                switch (propName)
                {
                    case "Izldl":
                        strjk = "JK-1,4";
                        break;

                    case "Ijldl":
                        strjk = "JK-1,2";
                        break;

                    case "Iypfs":
                    case "Idmh":
                        strjk = "JK-1,2";
                        break;

                    case "Iypjs":
                        strjk = "GL-1,1";
                        break;

                    case "Ibzd":
                        strjk = "JK-1,4";
                        break;
                }
                if (n + count < 34)
                {
                    strResule += port + "-" + n + "" + strjk + "," + count + "," + strkey + ";";
                    //tongjiDic[strkey].CgqNo += n;
                }
                else
                {
                    int n1 = (n + count) - 34;
                    strResule += port + "-" + n + "" + strjk + "," + (n - n1) + "," + strkey + ";";
                    strResule += GetNextPort(port) + "-2," + strjk + n1 + "," + strkey + ";";
                    //tongjiDic[strkey].CgqNo = 2 + n1;
                    //tongjiDic[strkey].TongXingJiPort = GetNextPort(port);
                }
            }
            return strjk;
        }

        private string GetJkbString(HuanXianClass hx)
        {
            string strResule = "";
            if (hx.Ibzd != 0)
            {
                strResule += GetJkbString(hx.Ibzd, "Ibzd");
            }
            if (hx.Idmh != 0)
            {
                strResule += GetJkbString(hx.Idmh, "Idmh");
            }
            if (hx.Igybdc != 0)
            {
                strResule += GetJkbString(hx.Igybdc, "Igybdc");
            }
            if (hx.Ijldl != 0)
            {
                strResule += GetJkbString(hx.Ijldl, "Ijldl");
            }
            if (hx.Iypfs != 0)
            {
                strResule += GetJkbString(hx.Iypfs, "Iypfs");
            }
            if (hx.Iypjs != 0)
            {
                strResule += GetJkbString(hx.Iypjs, "Iypjs");
            }
            if (hx.Izl != 0)
            {
                strResule += GetJkbString(hx.Izl, "Izl");
            }
            if (hx.Izldb != 0)
            {
                strResule += GetJkbString(hx.Izldb, "Izldb");
            }
            if (hx.Izldl != 0)
            {
                strResule += GetJkbString(hx.Izldl, "Izldl");
            }
            if (hx.Jldb != 0)
            {
                strResule += GetJkbString(hx.Jldb, "Jldb");
            }
            if (strResule == null)
                strResule = "";
            return strResule;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int sum = 0;
            inifileDy.WriteInt("电源模块", "sum", hxclassList.Count);
            TongJiClass tj = new TongJiClass(m_path + "\\统计.txt");
            foreach (HuanXianClass hx in hxclassList)
            {
                if (hx.IdymokuaiNo != 0)
                {
                    string sec = "电源模块" + hx.IdymokuaiNo;

                    string[] strs;
                    strs = GetZnCgqString(hx, tj);

                    sum++;

                    inifileDy.WriteString(sec, "智能传感器编号", strs[0]);
                    inifileDy.WriteString(sec, "接口板编号", strs[1]);
                }
            }
            inifileDy.WriteString("电源模块", "sum", sum.ToString());
            inifileDy.Save();
            WriteDataHx(tj);
            MessageBox.Show("ok");
        }

        private void WriteDataHx(TongJiClass tj)
        {
            string[] portNames = tj.GetPortNames();
            string sec = "";
            int a = 0;
            foreach (string s1 in portNames)
            {
                switch (s1)
                {
                    case "I":
                        sec = "接口1";
                        if (a < 1) a = 1;
                        break;

                    case "II":
                        sec = "接口2";
                        if (a < 2) a = 2;
                        break;

                    case "III":
                        sec = "接口3";
                        if (a < 3) a = 3;
                        break;

                    case "IV":
                        sec = "接口4";
                        if (a < 4) a = 4;
                        break;
                }
                string s3 = tj.GetPortCgqBianHao(s1);
                inifileData.WriteString(sec, "智能传感器编号", s3);
            }
            inifileData.WriteInt("通信机接口", "sum", a);
            inifileData.Save();
        }

        private void ultraGrid1_AfterCellUpdate(object sender, CellEventArgs e)
        {
            int n = 0;
            UltraGrid ulgrid = sender as UltraGrid;
            string str = ulgrid.ActiveCell.Column.Key;
            foreach (UltraGridRow r in ulgrid.Rows)
            {
                n += (int)r.GetCellValue(str);
            }
            //UpdateLable(str, n);
            Label lable;
            int val;
            GetLable_value(str, out lable, out val);
            if (lable != null)
            {
                if (n > val)
                {
                    lable.ForeColor = Color.Red;
                    lable.Text = val - n + "";
                }
                else
                {
                    lable.ForeColor = Color.Black;
                    lable.Text = val - n + "";
                }
            }
        }

        private void UpdateLable(string str, int n)
        {
            Label lable;
            int val;
            GetLable_value(str, out lable, out val);
            if (lable != null)
            {
                if (n > val)
                {
                    lable.ForeColor = Color.Red;
                    lable.Text = val - n + "";
                }
                else
                {
                    lable.ForeColor = Color.Black;
                    lable.Text = val - n + "";
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ReadHuanXianCfg();
        }

        private void ReadHuanXianCfg()
        {
            hxclassList.Clear();
            int sum = inifileDy.GetInt("电源模块", "sum", 0);
            for (int i = 0; i < sum; i++)
            {
                string str = inifileDy.GetString("电源模块" + (i + 1), "智能传感器编号");
                HuanXianClass hxclass = GetHuanXianClass(str);
                hxclass.IdymokuaiNo = i + 1;
                hxclassList.Add(hxclass);
            }
            ultraGrid1.SetDataBinding(hxclassList, "", true);
            int n = 0;

            UltraGridRow r = ultraGrid1.Rows[0];
            foreach (UltraGridColumn col in r.Band.Columns)
            {
                foreach (UltraGridRow r1 in ultraGrid1.Rows)
                {
                    n += (int)r1.GetCellValue(col.Key);
                }
                UpdateLable(col.Key, n);
                n = 0;
            }
        }

        private HuanXianClass GetHuanXianClass(string str)
        {
            HuanXianClass hxclass = new HuanXianClass();
            string[] strs = str.Split(new char[] { ',', ';', '-' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < (strs.Length - 1) / 4 + 1; i++)
            {
                try
                {
                    int n;
                    int.TryParse(strs[i * 4 + 2], out n);
                    hxclass.AppendPropertyValue(strDic[strs[i * 4 + 3]], n);
                }
                catch
                {
                }
            }
            return hxclass;
        }
    }
}